function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function setSession(key, value){
    if (key && value){
	$.ajax({
	  type: 'POST',
	  url: '/admins/debug/setsession',
	  data: {key: key, value: value},
	  success: function(){
	      return true;
	  }
	});
	
    }else{
	return false;
    }
}
function unsetSession(key){
    if (key){
	$.ajax({
	  type: 'POST',
	  url: '/admins/debug/unsetsession',
	  data: {key: key},
	  success: function(){
	      return true;
	  }
	});
	
    }else{
	return false;
    }
}
function checkIE(){
    var agt=navigator.userAgent.toLowerCase(); 
    var is_ie  = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    return is_ie;
}
function getIEVersion(){
    var version = 0;
		if (navigator.userAgent.indexOf ("MSIE 5") != -1)
			version = 5;
		else if (navigator.userAgent.indexOf("MSIE 4") != -1)
			version = 3;
		else if (navigator.userAgent.indexOf ("MSIE 4.5") != -1)
			version = 4;
		else if (navigator.userAgent.indexOf ("MSIE 6") != -1)
			version = 6;
		else if (navigator.userAgent.indexOf ("MSIE 7") != -1)
			version = 7;
		else if (navigator.userAgent.indexOf ("MSIE 8") != -1)
			version = 8;
		else if (navigator.userAgent.indexOf ("MSIE 9") != -1)
			version = 9;    
    return version;
}
function isScript(string){
    if (!string) return false;
    var head = document.getElementsByTagName('head');
	if (head.length > 0)
	    head = head[0];
	else
	    return false;
    var scripts = head.getElementsByTagName('script');
    if (scripts.length>0){
	for (var i=0; i<scripts.length; i++){
	    var script = (typeof(scripts[i].src)=='string')?(scripts[i].src):(null);
	    if (script!=null && script.indexOf(string)!=-1)
		return true;
	}
	return false;
    }else{
	return false;
    } 
}
function isStyle(string){
    if (!string) return false;
    var head = document.getElementsByTagName('head');
	if (head.length > 0)
	    head = head[0];
	else
	    return false;
    var styles = head.getElementsByTagName('link');
    if (styles.length>0){
	for (var i=0; i<styles.length; i++){
	    var style = (typeof(styles[i].href)=='string')?(styles[i].href):(null);
	    if (style!=null && style.indexOf(string)!=-1)
		return true;
	}
	return false;
    }else{
	return false;
    }
}
function attachScript(script, cache, check, checkByName, callback){
    if (typeof(script) == 'undefined') return false;
    if (typeof(script) == 'object'){
        for (var src in script){
            attachScript(script[src], cache, check, checkByName);
        }
        if (typeof(callback) == 'function') callback();
        return true;
    }
    if (check == true){
        var toCheck = script;
        if (checkByName == true)
            toCheck = script.replace(/^(.*)\//, '');
	var checked = isScript(toCheck);
    }
    if (checked == true) return false;
    var cache = cache || false;
    var head = document.getElementsByTagName('head')[0];
    if (cache == false)
    var script = script+'?'+(Math.random()*1000).toFixed(0);
    head.appendChild(cE('script',{src:script,type:'text/javascript'}));
    if (typeof(callback) == 'function') callback();
    return true;
}
function attachStyle(style, cache, media, check, checkByName){
    if (typeof(style) == 'undefined') return false;
    if (check == true){
        var toCheck = style;
        if (checkByName == true)
            toCheck = style.replace(/^(.*)\//, '');
	var checked = isStyle(toCheck);
    }
    if (checked == true) return false;
    var cache = cache || false;
    var media = media || 'screen';
    var head = document.getElementsByTagName('head')[0];
    if (cache == false)
    var style = style+'?'+(Math.random()*1000).toFixed(0);
    head.appendChild(cE('link',{type:'text/css',rel:'stylesheet',media:media, href:style}));
    return true;
}
function cE(tagname,options,style){
    var el = document.createElement(tagname);
    var IE = checkIE();
    var IEVersion = (IE)?(getIEVersion()):(null);
    if (options)
        for (var name in options){
            el[name]=options[name];
        }
    if (style)
        for (var name in style){						
	    
	    if (IE)
	    {
		var value = style[name];
                var styleOption = name;
                if (typeof(styleOption) == 'string' && styleOption == 'opacity'){
                    var opacity = value * 100;
                    el.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
                }
		if (typeof(value) == 'string'){
		    if (value.match(/rgba/)){
			var rgbaRegExp = new RegExp(/(.*)rgba\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3}),\s?([\d\.]{1,3})\)/);
			var rgb = '';
			if (rgbaRegExp.test(value)){
			    var array = value.match(rgbaRegExp);
			    var r = (array[2]*1)+(array[2]*array[5]).toFixed(0)*1;
			    var g = (array[3]*1)+(array[3]*array[5]).toFixed(0)*1;
			    var b = (array[4]*1)+(array[4]*array[5]).toFixed(0)*1;

			    rgb = array[1] + 'rgb(' + r + ',' + g + ',' + b + ')';
			    el.style[name] = rgb;
			}
		    }else{
			el.style[name] = style[name];
		    }
		}
	    }else{
		el.style[name] = style[name];
	    }
        }  
    return el;
}

function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0)
    {
        offset = cookie.indexOf(search);
        if (offset != -1)
        {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1)
            {
                end = cookie.length;
                }
        setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}

function toHex(tohex){
    if (typeof(tohex)!='number') return null;
    if (tohex==0) return 0;
    var hexarray=new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
    var hex=new Array;
    var i=0;
    while (tohex!=0){
        hex[i]=hexarray[tohex%16];
        i++;
        tohex=(tohex-tohex%16)/16;
    }
    hex.reverse()
    
    return hex.join('');
}

function unHex(unhex){
    var hexarray={'0':'0','1':'1','2':'2','3':'3','4':'4','5':'5','6':'6','7':'7','8':'8','9':'9','a':'10','b':'11','c':'12','d':'13','e':'14','f':'15'};
    var unhex=unhex.toLowerCase().split('');
    if (unhex.length>1){unhex.reverse();}
    var decimal=0;
    for (var i=0;i<unhex.length;i++){
        decimal+=(hexarray[unhex[i]])*Math.pow(16,i);
    }
    return decimal;
}

function inttostr(inttostr,amo){
    while (amo>inttostr.toString().length){
        inttostr="0"+inttostr.toString();
    }
    return inttostr.toString();
}
function switchColor(color){
    var color = color || null;
    var rgbRegExp = new RegExp(/rgb\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3})\)/);
    var hexRegExp = new RegExp(/\#([\da-f]{1,2})([\da-f]{1,2})([\da-f]{1,2})/);
    if (rgbRegExp.test(color)){
	var colors = color.match(rgbRegExp);
	var r = 255 - colors[1];
	var g = 255 - colors[2];
	var b = 255 - colors[3];
	return 'rgb(' + r + ', ' + g + ', ' + b + ')';
    }
    if (hexRegExp.test(color)){
	var colors = color.match(hexRegExp);
	var r = 255 - unHex(colors[1]);
	var g = 255 - unHex(colors[2]);
	var b = 255 - unHex(colors[3]);
	return '#' + toHex(r) + toHex(g) + toHex(b);
    }
    return false;
	
}
function rgbToHex(rgbColor){
    var rgbRegExp = new RegExp(/rgb\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3})\)/);
    if (rgbRegExp.test(rgbColor)){
	var colors = rgbColor.match(rgbRegExp);
	var r = toHex(colors[1]*1);
	var g = toHex(colors[2]*1);
	var b = toHex(colors[3]*1);
	return '#' + r + g + b;
    }else{
	return rgbColor;
    }
}
function hexToRgb(hexColor){
    var hexRegExp = new RegExp(/\#([\da-f]{1,2})([\da-f]{1,2})([\da-f]{1,2})/);
    if (hexRegExp.test(hexColor)){
	var colors = hexColor.match(hexRegExp);
	var r = unHex(colors[1]);
	var g = unHex(colors[2]);
	var b = unHex(colors[3]);
	return 'rgb(' + r + ', ' + g + ', ' + b + ')';
    }else{
	return hexColor;
    }
}
function deleteStyle(string){
    if (!string) return false;
    var head = document.getElementsByTagName('head');
	if (head.length > 0)
	    head = head[0];
	else
	    return false;
    var links = head.getElementsByTagName('link');
    var deleted = 0;
    if (links.length>0){
	for (var i=0; i<links.length; i++){
	    var style = (typeof(links[i].href)=='string')?(links[i].href):(null);
	    if (style!=null && style.indexOf(string)!=-1){
		$(links[i]).remove();
		deleted++;
	    }
	}
	if (deleted == 0) return false;
	else
	   return deleted; 
    }else{
	return false;
    }
}
function deleteScript(string){
    if (!string) return false;
    var head = document.getElementsByTagName('head');
	if (head.length > 0)
	    head = head[0];
	else
	    return false;
    var scripts = head.getElementsByTagName('script');
    var deleted = 0;
    if (scripts.length>0){
	for (var i=0; i<scripts.length; i++){
	    var script = (typeof(scripts[i].src)=='string')?(scripts[i].src):(null);
	    if (script!=null && script.indexOf(string)!=-1){
		$(scripts[i]).remove();
		deleted++;
	    }
		
	}
	if (deleted == 0) return false;
	else
	   return deleted; 
    }else{
	return false;
    }
}
function compareObjects(object1, object2){
    if (typeof(object1)=='object' && typeof(object2)=='object'){
	for (var key in object1){
	    if (typeof(object2[key])!='undefined'){
		if (object1[key]!=object2[key])
		    return false;
	    }else{
		return false;
	    }
	}
	return true;
    }
    return false;
}
function fromAMPM(string){
    var timeRegExp = new RegExp(/(\d{1,2}):(\d{1,2})\s(\D{2})/);
    if (timeRegExp.test(string)){
	var date = string.match(timeRegExp);
	var hour = date[1];
	var min = date[2];
	var ampm = date[3];

	if (ampm.toLowerCase() == 'pm'){
	    var hour = (hour == 12)?(0):(hour);
	    return inttostr(hour*1+12, 2) + ':' + inttostr(min, 2) + ':00';
	}else{
	    var hour = (hour == 12)?(0):(hour);
	    return inttostr(hour,2) + ':' + inttostr(min, 2) + ':00';
	}
    }else{
	return string;
    }
}
function toAMPM(string){
    var timeRegExp = new RegExp(/(\d{1,2}):(\d{1,2}):(\d{1,2})/);
    if (timeRegExp.test(string)){
	var date = string.match(timeRegExp);
	var hour = date[1];
	var min = date[2];
	var sec = date[3];
	if (hour>=12){
	    var hour = (hour-12 == 0)?(12):(hour-12);
	    return inttostr(hour, 2) + ':' + inttostr(min, 2) + ' pm';
	}else{
	    var hour = (hour == 0)?(12):(hour);
	    return inttostr(hour, 2) + ':' + inttostr(min, 2) + ' am';
	}
    }else{
	return string;
    }
}
function showObject(object, length, get){
    if (typeof(object) == 'object'){
        if (typeof(length) == 'undefined'){
            var length = 1;
        }
        var length = length.toString() || 1;
        var str = '<ul>';
        try {
            for (var key in object){
                var value = object[key];
                    key = '<b>' + key + '</b>:';
                if (typeof(value) == 'object' && length*1 != 0) {
                    value = showObject(value, (length-1)*1, 1)
                    key = key + '<br/>';
                }
                try{
                    str += '<li>' + key  + value + '</li>';
                }catch(e){
                    str += '<li>' + key + ' <i>Not available</i></li>';
                }
            }
        }catch(e){
            
        }
            str += '</ul>';
        if (get) return str;
        if (typeof(popUp) != 'undefined'){
            try{
                if (typeof(popupShowObject) != 'object'){
                    var popupShowObject = new popUp();
                }
                popupShowObject.load(str, {title: 'Object Struct', movable: true, minBtn: true, bgDisplay: false});
            }catch(e){
                alert(str);
            }    
        }else{
            alert (str);
        }
        return true;
    }else{
        return false;
    }
}
function truncateString(str, length, symbols){
    if (typeof(str) == 'string'){
        var length = length || 20;
        var symbols = symbols || '';
        var regExp = new RegExp("^([^.]{" + length + "})(.*)");
        return str.replace(regExp, "$1" + symbols);
    }else{
        return false;
    }
}
function lineString(str, length, separator){
    if (typeof(str) == 'string'){
        var length = length || 20;
        var separator = separator || '<br\/>';
        var separatorRegExp = new RegExp("(.*)" + separator + "(.*)");
        var spaceRegExp = new RegExp(/(.*)(\s)(.*)$/);
        var strLength = str.length;
        var constLength = length;
        var newstr = '';
        try {
            while (length < strLength){
                truncateRegExp = new RegExp("(.{" + (length*1 - constLength*1) + "})(.{0," + constLength + "})");
                var tempStr = str.match(truncateRegExp);
                if (tempStr && (tempStr[2] || tempStr[2])){
                    tempStr = (tempStr[2])?(tempStr[2]):(tempStr[1]);
                    if (!separatorRegExp.test(tempStr)){
                        var strArray = tempStr.match(spaceRegExp);
                        if (strArray){
                            tempStr = tempStr.replace(spaceRegExp, '$1' + separator + '$3');
                        }else{
                            tempStr = tempStr + separator;
                        }
                    }
                    str = str.replace(truncateRegExp, "$1" + tempStr);
                    var separatorPosition = tempStr.indexOf(separator);
                    if (separatorPosition != -1){
                        var countChars = separatorPosition*1 + separator.length*1;
                        length += countChars;
                    }else{
                        length += 10;
                    }
                    strLength = str.length;
                }else{
                    return str;
                }
            }
        }catch(e){};
        return str;
    }else{
        return false;
    }
}
function getErrorText(xmlHttp, textStatus, errorThrown){
    var error = 
        '<div style="padding: 10px">Oops! Some error occured. Please try again later.<br/>\n\
        <i>Error details: </i><br/>';
    if (typeof(xmlHttp) != 'undefined'){
        if (typeof(xmlHttp.status) != 'undefined'){
            error += 'Result Status: ' + xmlHttp.status + '<br/>';
        }
        if (typeof(xmlHttp.status) != 'undefined'){
            error += 'Status Text: ' + xmlHttp.statusText + '<br/>';
        }
        if (typeof(textStatus) != 'undefined'){
            error += 'Text Status: ' + textStatus + '<br/>';
        }
        if (typeof(errorThrown) != 'undefined'){
            error += 'Error Throw: ' + errorThrown + '<br/>';
        }
        if (typeof(xmlHttp.responseText) != 'undefined'){
            error += 'Responce Text: <div style="max-width: 600px; overflow: auto;"><pre>' + xmlHttp.responseText + '</pre></div></div>';
        }
    }       
    return error;
}
function getObjectsByClass(className, object){
    if (typeof(className) == 'function'){
        var objects = new Object();
        var object = object || window;
        for (var i in object){
            try{
                if (object[i] instanceof className){
                    objects[i] = object[i];
                }
            }catch(e){
                
            }
        }
        return objects;
    }else{
        return false;
    }
}
function createNode(object){
    if (typeof (object) == 'object'){
        var tagname = (object.tagname)?(object.tagname):null;
        var options = (object.options)?(object.options):null;
        var style = (object.style)?(object.style):null;
        var inner = (object.inner)?(object.inner):null;
        if (!tagname) return false;
        var el = cE(tagname, options, style);
        if (inner){
            if (typeof(inner) == 'object'){
                for (var i in inner){
                    if (typeof(inner[i]) == 'object'){
                        el.appendChild(createNode(inner[i]));
                    }
                    if (typeof(inner[i] == 'string')){
                        el.innerHTML = el.innerHTML + inner[i];
                    }
                }
                
            }

        }
        return el;
    }else{
        return false;
    }
}
function extend(){
    var object = {};
    for (var i = 0; i<arguments.length; i++ ){
        if (typeof(arguments[i]) == 'object'){
            var current = arguments[i];
            for (var key in current){
                object[key] = current[key];
            }
        }
    }
    return object;
}
function getFormErrors(target, title, type)
{   
    type = type || '';
    title = title || '';
    var empty = {length: 0};
    var errField;
    $('#' + target + ' input, #' + target + ' select, #' + target + ' textarea').each(function(){
        var max = 3;
        var container = this.parentNode;
        var labels = [];
        while(max>0){
           labels = container.getElementsByTagName('label');
           if (labels.length == 1){
               break;
           }else{
               container = container.parentNode;
               max--;
           }
       }
       if (labels.length == 1){
            var span = container.getElementsByTagName('span');
            var elements = $('input:not([type=hidden]), select, textarea', container).toArray();
            var value = elements[0].value;
                for (var element in elements){
                    if (value != '' && elements[element].value == '' && elements[element].type != 'hidden'){
                        value = elements[element].value;
                    }
                }
            if (span.length == 1 && (value == '' || value == 'empty') && container.style.display != 'none'){
                try{
                   errField = labels[0].innerHTML.match(/(.+):\s?/)[1];
                   if (typeof(empty[errField]) == 'undefined'){
                       empty[errField] = errField;
                       empty.length++;
                   }
               }catch(e){}
            }
       }
    });       
    if (empty.length>0) 
    {
        if (type == 'array'){
            return empty;
        }
        var li = '';
        for (var key in empty){
            if (key != 'length')
            li += '<li>' + empty[key] + '</li>'; 
        }
        if (type == 'text'){
            return li;
        }else{
            li = '<ul>' + li + '</ul>';
            fbNotify.showNotify({title: title, description: li, target: target, position: 'before', type: 'error', timeout: false});
        }
        return false;
    }    
    else
    {
        if (type == 'array'){
            return empty;
        }
        if (type == 'text'){
            return '';
        }
        return true;
    }    
}

function parseErrors(object){
    var html = '';
        
    if (typeof(object) == 'object' && $(object).size() > 0){
        html += '<ul>';
        for (var key in object){
            if (typeof(key) == 'string'){
                for (var item in object[key]){
                    if (typeof(item) == 'string'){
                        html += '<li><b>' + key + ': </b>' + object[key][item] + '</li>';
                    }
                }
            }
        }
        html += '</ul>';
    }
    return html;
}
